
from reference import weaponRef,  traitRef
from trait import trait
from Enumerate import Enumerate

class weapon:

    types = Enumerate('RANGED', 'THROWN', 'MELEE',  'INFANTRY')
    arcs = Enumerate('FRONT', 'REAR', 'LEFT', 'RIGHT', 'ARM', 'TURRET')
  
    # Tracks in what order we should be displayed
    sortOrder = 0

    def __init__(self,  name,  dict):        
        self.name = None
        self.type = self.types.RANGED    
        self.arc = self.arcs.FRONT
        self.arcS = "F"
        self.acc = 0

        self.rangePR = 0
        self.rangeCR = 0
        self.rangeER = 0

        self.dam = 0
        self.ammo = 0

        self.traits = []
        self.traitsS = ""
        #print "weapon:__init__:Setting name = %s" % (name)
        
        # Initialize the weapon with the default values for it's type
        if name in weaponRef:
            #print "weapon:__init__:Found reference for weapon [%s]" % (name)
            refObj = weaponRef[name]
                        
            self.name = name
            self.dam = refObj["dam"]
            if "acc" in refObj:
                self.acc = refObj["acc"]
            
            if "ammo" in refObj:
                self.ammo = refObj["ammo"]
            
            if "traits" in refObj:                
                traitsL = refObj["traits"].split(',')
                for traitS in traitsL:
                    if self.traitsS is not "":
                        self.traitsS += ", "
                    traitObj = trait(traitS.lower())
                    #print "TraitS = %s" % traitS
                    self.traits.append(traitObj)
                    self.traitsS += traitObj.label

            if "type" in refObj:                
                if refObj["type"].lower() == "thrown":
                    self.type = self.types.THROWN                    
                elif refObj["type"].lower() == "melee":
                    self.type = self.types.MELEE
                elif refObj["type"].lower() == "infantry":
                    self.type= self.types.INFANTRY
            else:
                self.type= self.types.RANGED
            
            if self.type is self.types.RANGED or self.type is self.types.INFANTRY:            
                self.rangeER = int(refObj["er"])
                #print "weapon:__init__:Self range is: " + str(self.rangeER)
                self.rangeCR = self.rangeER / 2
                self.rangePR= self.rangeER / 8
        
            if "sortOrder" in refObj:
                self.sortOrder = refObj["sortOrder"]
            
        else:
            print "weapon:__init__:Failed to find (%s) in refDict!" % name
        
        # Check for an empty dictionary
        if dict == None:
            return
        
        # Now do any legitimate overrides
        if "arc" in dict:
            arcS = dict["arc"].lower()
            #print "weapon:__init__:Overriding default arc with [%s]" % arcS
            if arcS == "rr":
                self.arc = self.arcs.REAR
                self.arcS = "Rr"
            elif arcS == "l":
                self.arc = self.arcs.LEFT
                self.arcS = "L"
            elif arcS == "r":
                self.arc = self.arcs.RIGHT
                self.arcS = "R"
            elif arcS == "a":
                self.arc = self.arcs.ARM
                self.arcS = "A"
            elif arcS.startswith("t"):
                self.arc = self.arcs.TURRET
                self.arcS = arcS
            else:
                self.arc = self.arcs.FRONT
                self.arcS = "F"
        
        if "ammo" in dict:
            self.ammo = dict["ammo"]
            #print "weapon:__init__:Overriding default ammo value with [%s]" % self.ammo
            
        if "traits" in dict:
            traitsL =  dict["traits"].split(',')
            for traitO in traitsL:
                #print "Trait = [%s]" % traitO
                if self.traitsS is not "":
                    self.traitsS += ", "
                traitObj = trait(traitO.lower())
                #print "TraitS = %s" % traitS
                self.traits.append(traitObj)
                self.traitsS += str(traitObj.label)

def weaponCmp(x,  y):
    if x.sortOrder > y.sortOrder:
        return 1
    elif x.sortOrder < y.sortOrder:
        return -1
    elif x.sortOrder == y.sortOrder:
        return cmp(x.name,  y.name)
